﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AutoCs
{
    class Program
    {
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern IntPtr GetForegroundWindow();
        [DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        public static extern int ShowWindow(IntPtr hwnd, int nCmdShow);

        public static IntPtr? Ptr;
        static void Main(string[] args)
        {
            CsSharpApi.WindowInfo[] windowInfos = CsSharpApi.GetAllDesktopWindows();
            List<CsSharpApi.WindowInfo> list =new List<CsSharpApi.WindowInfo>();
            Console.WriteLine("窗口:");
            for (int i = 0; i < windowInfos.Length; i++)
            {
                CsSharpApi.WindowInfo windowInfo = windowInfos[i];
                if (windowInfo.szWindowName.Contains("星战前夜"))
                {
                    Ptr = windowInfo.hWnd;
                    CsSharpApi.ClientToScreen(Ptr.Value, out Point pt);
                    Console.WriteLine($"窗口坐标:({pt.X},{pt.Y})");
                }
            }
            while (true && Ptr.HasValue)
            {
                Console.Write("->");
                string s = Console.ReadLine();
                s = s.ToLower();
                switch (s) { 
                    case "f1": {
                            Keyboard.Input.KeyDownOnce(Ptr.Value, Keyboard.KeyCode.F1);
                            break; 
                        }
                    case "f2":
                        {
                            Keyboard.Input.KeyDownOnce(Ptr.Value, Keyboard.KeyCode.F2);
                            break;
                        }
                    case "f3":
                        {
                            Keyboard.Input.KeyDownOnce(Ptr.Value, Keyboard.KeyCode.F3);
                            break;
                        }
                    case "capture":
                        {
                           Screen.ScreenCapture.CaptureForm(Ptr.Value);
                           break;
                        }
                    case "text": {
                            StringBuilder stringBuilder = new StringBuilder();
                            CsSharpApi.GetWindowTextW(Ptr.Value,stringBuilder,65535);
                            Console.WriteLine(stringBuilder.ToString());
                            break;
                        }
                    case "read": {
                            break; 
                        }
                    case "click": {
                            Keyboard.Input.KeyGroupDown(Ptr.Value,new Keyboard.KeyCode[] { Keyboard.KeyCode.Alt , Keyboard.KeyCode.E });
                            Console.WriteLine("执行成功");
                            break;                            
                        }
                    default: break;
                }
            }
        }
    }
}
